<?php
/**
 * 站点管理
 * 
 * @author xiaodu
 * @category  
 * @package ControllerSiteManger
 */
class ControllerSiteManger extends Controller {
	public function index() {
		$this->document->setTitle($this->config->get('config_title').'添加站点');
		$this->document->setDescription($this->config->get('config_meta_description'));
		$this->data['heading_title'] = '添加站点';

        $this->data['opt_category'] = $this->outputCategoryList();
		
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'site/add.tpl')) {
			$this->template = $this->config->get('config_template') . 'site/add.tpl';
		} else {
			$this->template = 'site/add.tpl';
		}
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		$this->response->setOutput($this->render());
	}
    
    // 添加新站点
    public function insert()
    {
        $json = array();
        if (isset($this->request->post['site_name'])) 
        {
            $site_name = $this->request->post['site_name'];
        }
        else
        {
            $site_name='';
        }
        if (isset($this->request->post['site_url'])) 
        {
            $site_url = $this->request->post['site_url'];
        }
        else
        {
            $site_url='';
        }
        if (isset($this->request->post['cat_id'])) 
        {
            $cat_id = $this->request->post['cat_id'];
        }
        else
        {
            $cat_id = '';
        }
            
        if (isset($this->request->post['account'])) 
        {
            $account_info = $this->request->post['account'];
        }
        else
        {
            $account_info = '';
        }            
        if (isset($this->request->post['other'])) 
        {
            $other = $this->request->post['other'];
        }
        else
        {
            $other = '';
        }
    
        $data = array(
            'site_url' => $site_url,
            'site_name' => $site_name,
            'cat_id' => $cat_id,
            'account' => $account_info,
            'other' => $other,
        );
        
        $this->load->model('site');
        $result = $this->model_site->insert($data);
        if($result)
        {
            $json['success'] = '添加站点成功';
        }
        
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }
    
    // 编辑站点
    public function edit()
    {
		$this->document->setTitle($this->config->get('config_title').'编辑站点');
		$this->document->setDescription($this->config->get('config_meta_description'));
		$this->data['heading_title'] = '编辑站点';
        $siteinfo = array();
        $this->load->model('site');
        if (isset($this->request->get['site_id'])) 
        {
            $site_id = $this->request->get['site_id'];
        }
        else
        {
            $site_id = 0;
        }
        $this->data['site_id'] = $site_id;
        $siteinfo = $this->model_site->getSiteInfo($site_id);

        $this->data['site_url'] = $siteinfo['site_url'];
        $this->data['site_name'] = $siteinfo['site_name'];
        $this->data['account'] = $siteinfo['account'];
        $this->data['other'] = $siteinfo['other'];
        $this->data['opt_category'] = $this->outputCategoryList();
        
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'site/edit.tpl')) {
			$this->template = $this->config->get('config_template') . 'site/edit.tpl';
		} else {
			$this->template = 'site/edit.tpl';
		}
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		$this->response->setOutput($this->render());
    }
    
    // 更新站点内容
    public function update()
    {
        $json = array();
        if (isset($this->request->post['site_name'])) 
        {
            $site_name = $this->request->post['site_name'];
        }
        else
        {
            $site_name='';
        }
        if (isset($this->request->post['site_url'])) 
        {
            $site_url = $this->request->post['site_url'];
        }
        else
        {
            $site_url='';
        }
        if (isset($this->request->post['cat_id'])) 
        {
            $cat_id = $this->request->post['cat_id'];
        }
        else
        {
            $cat_id = '';
        }
                
        if (isset($this->request->post['site_id'])) 
        {
            $site_id = $this->request->post['site_id'];
        }
        else
        {
            $site_id = '';
        }

        if (isset($this->request->post['account'])) 
        {
            $account_info = $this->request->post['account'];
        }
        else
        {
            $account_info = '';
        }            
        if (isset($this->request->post['other'])) 
        {
            $other = $this->request->post['other'];
        }
        else
        {
            $other = '';
        }
        
        $data = array(
            'site_id' => $site_id,
            'site_url' => $site_url,
            'site_name' => $site_name,
            'cat_id' => $cat_id,
            'account' => $account_info,
            'other' => $other,
        );
        
        $this->load->model('site');
        $this->model_site->update($data);
        $json['success'] = '站点更新成功';
        //$this->redirect($this->url->link('category/manger', '', 'SSL'));
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
        
    }
    
    public function delete()
    {
        $json = array();
        if (isset($this->request->post['site_id'])) 
        {
            $this->load->model('site');
            $this->model_site->delete($this->request->post['site_id']);
            
            $json['success'] = '删除成功';
        }
    
        //$this->data['redirecturl'] = $this->url->link('site/list', '', 'SSL')
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }

    public function getTitle()
    {
        $json = array();
        if (isset($this->request->get['site_id'])) 
        {
            $site_id = $this->request->get['site_id'];
        }
        else
        {
            $site_id = 1;
        }

        $this->load->model('site');
        $siteinfo = $this->model_site->getSiteInfo($site_id);
        
        $lines = file_get_contents($siteinfo['site_url']);  
        preg_match("/<title>(.*)<//title>/smUi",$lines, $matches);    
        
        /* to do */
        
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }
    
    public function checkSiteStatus()
    {
        $json = array();
        $this->load->library('json');
        
        if (isset($this->request->get['site_id'])) 
        {
            $site_id = $this->request->get['site_id'];
        }
        else
        {
            $site_id = 1;
        }
        
        if(!$site_id)
        {
            $json['error']['warning'] = '网站不存在';
            $this->response->setOutput(Json::encode($json));
        }
        else
        {
            $this->load->model('site');
            $siteinfo = $this->model_site->getSiteInfo($site_id);
            
            $host_ip = gethostbyname($siteinfo['site_url']);
            echo $host_ip;
            $tcpconn = @fsockopen($host_ip,80,$errno,$errstr,10);
            if(!$tcpconn)
            {
                $json['error']['warning'] = '检测不通';
            }
            else
            {
                $json['success'] = 'ok';
            }
            $this->response->setOutput(Json::encode($json));
        }
    }
    
    
    public function getPageRank()
    {
        $json = array();
        define('PR_API_KEY_1','WooyqrEnuljuoUBC1MWMzNBJh3vzTYpqob990XqnXQWPBSoRgMQrIrQDIiA7CTWj');
        define('PR_API_KEY_2','8yVBf4f65AIvEArEIItelaUcnm9FvtbkGKMhs6uA29iok0t1Jjecg5LRqvYTRuCR');
        $rand = mt_rand(1,2); 
        if (isset($this->request->get['site_id'])) 
        {
            $site_id = $this->request->get['site_id'];
        }
        else
        {
            $site_id = '';
        }
        
        $this->load->model('site');
        $siteinfo = $this->model_site->getSiteInfo($site_id);
        
        $url = 'http://api.exslim.net/pagerank?url=' . $siteinfo['site_url'] . '&key=' . PR_API_KEY_1 . '&return=raw';
        $rand = mt_rand(1,2); 
        $pr = file_get_contents($url);
        if($pr >= 0 )
        {
            $json['pagerank'] = $pr;
        }
        else
        {
            $json['error']['warning'] = '获取PageRank失败';
        }
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }
    
    public function updatePageRank()
    {
        $this->load->model('site');
        if (isset($this->request->post['site_id'])) 
        {
            $site_id = $this->request->post['site_id'];
        }
        else
        {
            $site_id = 0;
        }
                
        if (isset($this->request->post['pagerank'])) 
        {
            $pagerank = $this->request->post['pagerank'];
        }
        else
        {
            $pagerank = 0;
        }
        
        $this->model_site->updatePageRank($site_id,$pagerank);
        $json = array();
        $json['success'] = '更新PageRank成功';
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }
    
    function checkUrlexist()
    {
        $this->load->model('site');
        if (isset($this->request->get['site_url'])) 
        {
            $site_url = $this->request->get['site_url'];
        }

        $json = array();
		$short_url = str_replace('http://', '', $site_url);
		$short_url = str_replace('www.', '', $short_url);
		if ('/' == substr($short_url, -1))
		$short_url = substr($short_url, 0, -1);
        $is_exist = $this->model_site->check($short_url);
        if($is_exist)
        {
            $json['success'] = '网址已经存在';
        }
        
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }

    public function move()
    {
        $this->load->model('site');
    
        if (isset($this->request->post['selected']))
        {
            $json['selected'] = $this->request->post['selected'];
            $this->load->library('json');
            $this->response->setOutput(Json::encode($json));
        }
        else
        {
            $json['error']['warning'] = '没有选择站点';
            $this->load->library('json');
            $this->response->setOutput(Json::encode($json));
        }
        
    }
    
    public function changeCategory()
    {
            if (isset($this->request->post['cat_id'])) 
            {
                $cat_id = $this->request->post['cat_id'];
            }
            else
            {
                $cat_id = 3;
            }
            $sites = implode(',',$this->request->post['selected']);
            foreach ($this->request->post['selected'] as $site_id) 
            {
				$this->model_site->changeCategory($site_id,$cat_id);
	  		}

            $json['success'] = '转移成功';
            $this->load->library('json');
            $this->response->setOutput(Json::encode($json));
    }
    
    private function outputCategoryList()
    {
        $output = '';
        $this->load->model('site');
        $this->load->model('category');
        $categories = $this->model_category->getCategories();

        foreach($categories as $category)
        {
            
            $output .= '<option value="' . $category['cat_id'] . '"';
           
            if (isset($this->request->get['site_id'])) 
            {
                $siteinfo = $this->model_site->getSiteInfo($this->request->get['site_id']);
                if($siteinfo['cat_id'] == $category['cat_id'])
	      		$output .= ' selected="selected"';
	    	}
	
	    	$output .= '>' . $category['cat_name'] . '</option>';
        }
		
		if (!$categories) {
		  	$output .= '<option value="0">没有选择项</option>';
		}
        
        return $output;
    }
    
}
?>